package cn.tedu.csmall.passport.controller;


import cn.tedu.csmall.passport.web.JsonResult;
import cn.tedu.csmall.passport.web.ServiceCode;
import cn.tedu.csmall.passport.ex.ServiceException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler
    public JsonResult handleServiceException(ServiceException e){

       // return JsonResult.fail(e.getState(),e.getMessage());
        return JsonResult.fail(e);
    }

    @ExceptionHandler
    public JsonResult handleBindException(BindException e) {
        // 使用以下方式时，如果有多个检查错误，
        // 将获取其中某1个检查注解中配置的message值，但无法确定到底是哪一个
         String message = e.getFieldError().getDefaultMessage();

        // 使用以下方式时，如果有多个检查错误，将可以反馈所有错误的信息
//        StringJoiner stringJoiner = new StringJoiner("，", "请求参数格式错误，", "！");
//        List<FieldError> fieldErrors = e.getFieldErrors();
//        for (FieldError fieldError : fieldErrors) {
//            stringJoiner.add(fieldError.getDefaultMessage());
//        }
//        return stringJoiner.toString();
        return JsonResult.fail(ServiceCode.ERR_BAD_REQUEST, message);
    }
    @ExceptionHandler
    public String handleConstraintViolationException(ConstraintViolationException e) {
        String message = null;
        Set<ConstraintViolation<?>> constraintViolations = e.getConstraintViolations();
        for (ConstraintViolation<?> constraintViolation : constraintViolations) {
            message = constraintViolation.getMessage();
        }
        return message;
    }

    @ExceptionHandler({
            InternalAuthenticationServiceException.class,
            BadCredentialsException.class
    })
    public JsonResult handleAuthenticationException(AuthenticationException e) {
        String message = "登录失败，用户名或密码错！";
        log.warn(message);
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED, message);
    }

    @ExceptionHandler
    public JsonResult handleDisabledException(DisabledException e) {
        String message = "登录失败，此账号已经被禁用！";
        log.warn(message);
        return JsonResult.fail(ServiceCode.ERR_UNAUTHORIZED_DISABLED, message);
    }
    @ExceptionHandler
    public JsonResult handleAccessDeniedException(AccessDeniedException e) {
        String message = "禁止访问，当前登录的账号无此操作权限！";
        log.warn(message);
        return JsonResult.fail(ServiceCode.ERR_FORBIDDEN, message);
    }
    @ExceptionHandler
    public JsonResult handleThrowable(Throwable e){
        String message = "服务器繁忙，请稍后再试或联系管理员处理！";
        log.warn("出现了Throwable异常");
        log.warn("异常类型:{}",e.getClass().getName());
        log.warn("异常信息:{}",e.getMessage());
        log.warn("异常跟踪信息如下：",e);
        return JsonResult.fail(ServiceCode.ERR_UNKNOWN,message);
    }



}
